<%@page import="java.util.HashSet"%>
<%@page import="com.dilean.api.model.AttributeListValue"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%-- imports --%>
<%@page import="com.dilean.api.dao.AttributeTypeDAO"%>
<%@page import="com.dilean.impl.model.AttributeTypeImpl"%>
<%@page import="com.dilean.api.model.AttributeType"%>
<%@page import="com.dilean.spring.AplicationContextHolder"%>
<%@page import="com.dilean.api.dao.PersonDAO"%>
<%@page import="com.dilean.impl.model.PersonImpl"%>
<%@page import="com.dilean.api.model.Person"%>
<%@page import="com.dilean.impl.model.AttributeListValueImpl"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueDate"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueList"%>
<%@page import="java.math.BigInteger"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueTextMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueText"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueNumber"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueNumberMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueText"%>
<%@page import="java.util.Collection"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="com.dilean.spring.AplicationContextHolder"%>
<%@page import="com.dilean.api.dao.PersonDAO"%>
<%@page import="com.dilean.impl.model.PersonImpl"%>
<%@page import="com.dilean.api.model.Person"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueListMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueList"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueDate"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueNumberMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueTextMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueText"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>

        <%
            int myId = 1;
            //Integer.parseInt(request.getParameter("id"));

            Person me = new PersonImpl();

            me = AplicationContextHolder.getInstance().getContext().getBean(PersonDAO.class).findByIdWithFriends(myId);

            AttrValueText lastName = me.getAttributeValue(2, AttrValueText.class);
            AttrValueTextMultiple emails = me.getAttributeValue(3, AttrValueTextMultiple.class);
            AttrValueNumberMultiple telephone = me.getAttributeValue(4, AttrValueNumberMultiple.class);
            AttrValueDate birthDate = me.getAttributeValue(10, AttrValueDate.class);
            AttrValueText myStatus = me.getAttributeValue(5, AttrValueText.class);
            AttrValueList myGender = me.getAttributeValue(6, AttrValueList.class);
            AttrValueList relStatus = me.getAttributeValue(7, AttrValueList.class);
            AttrValueList myUnivercity = me.getAttributeValue(9, AttrValueList.class);
            AttrValueListMultiple languages = me.getAttributeValue(8, AttrValueListMultiple.class);
            AttrValueText passw = me.getAttributeValue(13, AttrValueText.class);

            Collection<Person> friendRequests = me.getFriendRequests();
            Collection<Person> friends = me.getFriends();
            int numberRequest = 0;
            for (Person fr : friendRequests) {
                if (friends.contains(fr) == false) {
                    numberRequest++;
                }
            }

            String first_name = request.getParameter("first_name");
            String last_name = request.getParameter("last_name");
            String status = request.getParameter("status");
            int day = Integer.parseInt(request.getParameter("day"));
            int month = Integer.parseInt(request.getParameter("month"));
            int year = Integer.parseInt(request.getParameter("year"));
            String gender = request.getParameter("gender");
            String RSStatus = request.getParameter("RSStatus");
            String univercity = request.getParameter("univercity");
            String password = request.getParameter("password");
        %>
        <div id="wrapper">
            <!-- Sidebar -->
            <div id="sidebar-wrapper">
                <ul class="sidebar-nav">
                    <li class="sidebar-brand">
                        <a href="http://localhost:8080/Dilean/myPage.jsp">
                            <h2 align="centre" >Dilean</h2>
                        </a>
                    </li>
                    <li>
                        <a href="http://localhost:8080/Dilean/myPage.jsp"><i class="fa fa-home fa-fw-5x"></i>&nbsp; My Page</a>
                    </li>
                    <li>
                        <a href="http://localhost:8080/Dilean/friends.jsp?id=1"><i class="fa fa-users"></i>&nbsp; Friends </a>
                    </li>

                    <li>
                        <a href="http://localhost:8080/Dilean/allUsers.jsp?id=1"><i class="fa fa-database"></i>&nbsp; Users 
                            <%
                                if (numberRequest != 0) {
                            %>
                            <span class="badge badge-info"><%=numberRequest%> </span>
                            <%
                                }
                            %>
                        </a>
                    </li>



                    <li>
                        <a href="http://localhost:8080/Dilean/conversation.jsp"><i class="fa fa-envelope"></i>&nbsp; Messages </a>
                    </li>
                    <li class="divider"></li>
                    <li>
                        <a href="http://localhost:8080/Dilean/editUser.jsp?id=1"><i class="fa fa-cog"></i>&nbsp; Settings</a>
                    </li>
                    <li>
                        <a href="#"><i class="fa fa-book"></i>&nbsp; About</a>
                    </li>
                    <li>
                        <a href="#"><i class="fa fa-sign-out"></i>&nbsp; LogOut</a>
                    </li>

                </ul>
            </div>
            <!-- /#sidebar-wrapper -->


            <!-- Page Content -->
            <div id="page-content-wrapper">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-lg-12">

                            <a class="btn btn-large btn-primary" href="#menu-toggle" id="menu-toggle" >
                                <i class="fa fa-user fa-3x"></i> <h4>User Menu</h4></a>



                            <%
                                if (first_name.length() != 0) {

                                    AttrValueText name = me.getAttributeValue(1, AttrValueText.class);
                                    name.setValue(first_name);
                                    me.setParameter(1, name);
                                    me.setParameterForUpdate(1, name);


                            %>
                            <h1>first_name   <%=first_name%></h1>
                            <br>
                            <%

                                }

                                if (last_name.length() != 0) {
                                    lastName.setValue(last_name);
                                    me.setParameter(2, lastName);
                                    me.setParameterForUpdate(2, lastName);

                            %>
                            <h1>last_name   <%=last_name%></h1>
                            <br>
                            <%

                                }

                                if (status.length() != 0) {
                                    myStatus.setValue(status);
                                    me.setParameter(5, myStatus);
                                    me.setParameterForUpdate(5, myStatus);
                            %>
                            <h1>status   <%=status%></h1>
                            <br>
                            <%

                                }

                                if (password.length() != 0) {
                                    passw.setValue(password);
                                    me.setParameter(13, myStatus);
                                    me.setParameterForUpdate(13, myStatus);
                            %>
                            <h1>password   <%=password%></h1>
                            <br>
                            <%

                                }

                                if (birthDate.getValue().getDay() != day) {
                                    birthDate.getValue().setDate(day);
                                    me.setParameter(10, birthDate);
                                    me.setParameterForUpdate(10, birthDate);
                                    AplicationContextHolder.getInstance().getContext().getBean(PersonDAO.class).save(me);
                            %>
                            <h1>day   <%=day%></h1>
                            <br>
                            <%

                                }

                                if (birthDate.getValue().getMonth() != month) {
                                    birthDate.getValue().setMonth(month);
                                    me.setParameter(10, birthDate);
                                    me.setParameterForUpdate(10, birthDate);
                                    AplicationContextHolder.getInstance().getContext().getBean(PersonDAO.class).save(me);
                            %>
                            <h1>month   <%=month%></h1>
                            <br>
                            <%

                                }

                                if (birthDate.getValue().getYear() != year % 100) {
                                    birthDate.getValue().setYear(month + 1900);
                                    me.setParameter(10, birthDate);
                                    me.setParameterForUpdate(10, birthDate);
                                    AplicationContextHolder.getInstance().getContext().getBean(PersonDAO.class).save(me);
                            %>
                            <h1>month   <%=month + 1900%></h1>
                            <br>
                            <%

                                }

                                if (myGender.getValue().getName().compareTo(gender) != 0) {

                            %>
                            <h1>gender   <%=gender%></h1>
                            <br>
                            <%

                                }

                                if (relStatus.getValue().getName().compareTo(RSStatus) != 0) {
                            %>
                            <h1>RSStatus   <%=RSStatus%></h1>
                            <br>
                            <%

                                }

                                if (myUnivercity.getValue().getName().compareTo(univercity) != 0) {
                            %>
                            <h1>univercity   <%=univercity%></h1>
                            <br>
                            <%

                                }
                            %>     




                            <%
                                AplicationContextHolder.getInstance().getContext().getBean(PersonDAO.class).save(me);
                            %>
















                            <!-- /user Profile -->




                            <script>
                                $("#menu-toggle").click(function(e) {
                                    e.preventDefault();
                                    $("#wrapper").toggleClass("toggled");
                                });
                            </script>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>
